﻿using System;

namespace Common
{
    /// <summary>
    /// 结果类
    /// </summary>
    public class Result
    {
        public static Result Create(int resultCode)
        {
            return Create(resultCode,"");
        }

        public static Result Create(int resultCode,string message)
        {
            return Create(resultCode, message, null);
        }

        public static Result Create(int resultCode, string message, object data)
        {
            return new Result(resultCode,message,data);
        }

        public Result(){}

        public Result(int resultCode)
        {
            this.ResultCode = resultCode;
        }

        public Result(int resultCode, string message)
        {
            this.ResultCode = resultCode;
            this.Message = message;
        }

        public Result(int resultCode, string message, object data)
        {
            this.ResultCode = resultCode;
            this.Message = message;
            this.Data = data;
        }

        public int ResultCode { get; set; } = CommonCode.RESULT_OK;

        public string Message { get; set; }
        public object Data { get; set; }

        /// <summary>
        /// 是否成功
        /// </summary>
        /// <returns></returns>
        public bool IsSuccess()
        {
            return ResultCode == CommonCode.RESULT_OK;
        }

    }


    /// <summary>
    /// 结果类的范型实现
    /// </summary>
    /// <typeparam name="T">结果数据类型</typeparam>
    public class Result<T>
    {
        public static Result<T> Create(int resultCode)
        {
            return Create(resultCode, "");
        }

        public static Result<T> Create(int resultCode, string message)
        {
            return Create(resultCode, message, default(T));
        }

        public static Result<T> Create(int resultCode, string message, T data)
        {
            return new Result<T>(resultCode, message, data);
        }

        public Result() { }

        public Result(int resultCode)
        {
            this.ResultCode = resultCode;
        }

        public Result(int resultCode, string message)
        {
            this.ResultCode = resultCode;
            this.Message = message;
        }

        public Result(int resultCode, string message, T data)
        {
            this.ResultCode = resultCode;
            this.Message = message;
            this.Data = data;
        }

        public int ResultCode { get; set; } = CommonCode.RESULT_OK;

        public string Message { get; set; }
        public T Data { get; set; }

        /// <summary>
        /// 是否成功
        /// </summary>
        /// <returns></returns>
        public bool IsSuccess()
        {
            return ResultCode == CommonCode.RESULT_OK;
        }
    }
}
